package com.tencent.cloud.iov.util;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * @author showxiao
 * @date 2018/10/11
 */
public final class InputMethodUtils {

    private static Context sContext;
    private static InputMethodManager sInputMethodManager;

    static {
        sContext = UtilServices.getContext();
    }

    private InputMethodUtils() {}

    public static void showInputMethod(View view) {
        showInputMethod(view, 0);
    }

    public static void showInputMethod(View view, int flags) {
        getInputMethodManager().showSoftInput(view, flags);
    }

    public static void hideInputMethod(View view) {
        getInputMethodManager().hideSoftInputFromWindow(
            view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN
        );
    }

    private static InputMethodManager getInputMethodManager() {
        if (sInputMethodManager == null) {
            sInputMethodManager = (InputMethodManager) sContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        return sInputMethodManager;
    }

}
